<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>部门管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .layui-card {border:1px solid #f2f2f2;border-radius:5px;}
        .icon {margin-right:10px;color:#1aa094;}
        .icon-cray {color:#ffb800!important;}
        .icon-blue {color:#1e9fff!important;}
        .icon-tip {color:#ff5722!important;}
        .layuimini-qiuck-module {text-align:center;margin-top: 10px}
        .layuimini-qiuck-module a i {display:inline-block;width:100%;height:60px;line-height:60px;text-align:center;border-radius:2px;font-size:30px;background-color:#F8F8F8;color:#333;transition:all .3s;-webkit-transition:all .3s;}
        .layuimini-qiuck-module a cite {position:relative;top:2px;display:block;color:#666;text-overflow:ellipsis;overflow:hidden;white-space:nowrap;font-size:14px;}
        .welcome-module {width:100%;height:210px;}
        .panel {background-color:#fff;border:1px solid transparent;border-radius:3px;-webkit-box-shadow:0 1px 1px rgba(0,0,0,.05);box-shadow:0 1px 1px rgba(0,0,0,.05)}
        .panel-body {padding:10px}
        .panel-title {margin-top:0;margin-bottom:0;font-size:12px;color:inherit}
        .label {display:inline;padding:.2em .6em .3em;font-size:75%;font-weight:700;line-height:1;color:#fff;text-align:center;white-space:nowrap;vertical-align:baseline;border-radius:.25em;margin-top: .3em;}
        .layui-red {color:red}
        .main_btn > p {height:40px;}
        .layui-bg-number {background-color:#F8F8F8;}
        .layuimini-notice:hover {background:#f6f6f6;}
        .layuimini-notice {padding:7px 16px;clear:both;font-size:12px !important;cursor:pointer;position:relative;transition:background 0.2s ease-in-out;}
        .layuimini-notice-title,.layuimini-notice-label {
            padding-right: 70px !important;text-overflow:ellipsis!important;overflow:hidden!important;white-space:nowrap!important;}
        .layuimini-notice-title {line-height:28px;font-size:14px;}
        .layuimini-notice-extra {position:absolute;top:50%;margin-top:-8px;right:16px;display:inline-block;height:16px;color:#999;}
    </style>
</head>
<body>
<div class="layuimini-container" style="height: 700px">
    <div class="layuimini-main">
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <button id="insert" class="layui-btn layui-btn-xs">新增</button>
        <button id="update" class="layui-btn layui-btn-xs">保存</button>
        <button id="delete" class="layui-btn layui-btn-xs">删除</button>
        <hr class="layui-border-green">

        <div style="padding: 15px;">
            <div style="overflow: auto; float: left;width: 300px;height: 550px" id="test1" class="demo-tree demo-tree-box"></div>
            <div style="float:left;margin-left: 20px" id="test2">
                <div class="layui-form-item layui-form-pane">
                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">上级部门</label>
                        <div class="layui-input-inline">
                            <input id="topDep" placeholder="组织机构" type="text" class="layui-input" readonly>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label"><span style="color: red">*</span>部门名称</label>
                        <div class="layui-input-inline">
                            <input type="text" id="departmentName" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div id="main" style="width: 600px;height:500px;float: right;top: 100px"></div>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="../../js/echarts.js"></script>
<script src="../../js/userAuth.js"></script>
<!--初始化-->
<script>
    var myArray = [];
    $(function () {
        $.ajax({
            url : "/depAndJob/depTree",
            type:"get",
            success: function(response) {
                myArray = response;
                console.log(myArray)
                layui.use(['tree', 'util'], function(){
                    var tree = layui.tree
                        ,layer = layui.layer
                        ,util = layui.util
                        //模拟数据1
                        ,data1 = [{
                            title: '组织机构'
                            ,id: 1111
                            ,spread: true
                            ,children: myArray
                        }];
                    //常规用法
                    tree.render({
                        elem: '#test1'
                        ,onlyIconControl: true//默认是点击节点可进行收缩
                        ,data: data1
                        //,edit: ['add', 'update', 'del'] //操作节点的图标
                        ,click: function(obj){

                            var nodes = document.getElementsByClassName("layui-tree-txt");
                            //节点高亮
                            for (var i = 0; i < nodes.length; i++) {
                                if (nodes[i].innerHTML === obj.data.title) {
                                    nodes[i].style.color = "#009688";
                                    nodes[i].style.fontWeight = "bold";
                                } else {
                                    nodes[i].style.color = "#555";
                                    nodes[i].style.fontWeight = "normal";
                                }
                            }
                            console.log(obj.data); //得到当前点击的节点数据
                            console.log(obj.state); //得到当前节点的展开状态：open、close、normal
                            console.log(obj.elem); //得到当前节点元素
                            //console.log(obj.data.children); //当前节点下是否有子节点
                            if (obj.data.children == null) {
                                console.log("部门被选中");
                                $("#departmentName").val(obj.data.title)
                            }else {
                                $("#departmentName").val('')
                            }
                            $("#insert").click(function() {
                                if ($("#departmentName").val() === '') {
                                    layer.msg("请输入部门名称！",{time:500})
                                }else {
                                    $.ajax({
                                        url: '/department/insert',
                                        method:'post',
                                        contentType:"application/json;charset=utf-8",
                                        datatype:"text",
                                        data:JSON.stringify({
                                            departmentName:$("#departmentName").val()
                                        }),
                                        success : function(response) {
                                            layer.msg(response.msg)
                                            setTimeout(function (){
                                                window.location.reload();
                                            }, 500);
                                        },
                                        error : function(response) {
                                            layer.msg(response.msg)
                                        }
                                    })
                                }
                            })
                            $("#delete").click(function() {
                                if ($("#departmentName").val() === '') {
                                    layer.msg("请选择一个部门！",{time:500})
                                }else {
                                    layer.confirm("确认要删除吗？", { title: "删除确认" },function() {
                                        $.ajax({
                                            url: '/department/deleteById',
                                            method: 'post',
                                            datatype: "text",
                                            data: {id: obj.data.id},
                                            success: function (response) {
                                                layer.msg(response.msg)
                                                setTimeout(function (){
                                                    window.location.reload();
                                                }, 500);
                                            },
                                            error: function (response) {
                                                layer.msg(response.msg)
                                            }
                                        })
                                    })
                                }
                            })
                            $("#update").click(function() {
                                if (obj.data.id === 1111) {
                                    layer.msg("请选择一个部门！",{time:500})
                                }else {
                                    if ($("#departmentName").val() !== '') {
                                        $.ajax({
                                            url: '/department/updateById',
                                            method: 'post',
                                            contentType:"application/json;charset=utf-8",
                                            datatype: "text",
                                            data: JSON.stringify({
                                                departmentId: obj.data.id,
                                                departmentName: $("#departmentName").val()
                                            }),
                                            success: function (response) {
                                                layer.msg(response.msg)
                                                setTimeout(function (){
                                                    window.location.reload();
                                                }, 500);
                                            },
                                            error: function (response) {
                                                layer.msg(response.msg)
                                            }
                                        })
                                    }
                                }
                            })
                        }
                    });
                });
            }
        })
    })
</script>
<!--圆形图渲染-->
<script>
    var resData;
    $(function () {
        $.ajax({   // 异步请求   想象成线程状态
            url:"/staff/selectForDepartment",   // 请求路径
            //type:"get",            // 请求的方式，不区分大小写   传递json不能使用get请求，否则返回400错误
            //datatype:"text",
            success:function(response){                        // 后台服务响应成功，自动返回数据
                resData = response.data;
                console.log("数据为：  "+JSON.stringify(response.data))
                // 基于准备好的dom，初始化echarts实例
                var myChart = echarts.init(document.getElementById('main'));
                var option = {
                    title: {
                        text: '企业人员分布',
                        //subtext: '企业人员分布',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item'
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left'
                    },
                    series: [
                        {
                            name: 'Access From',
                            type: 'pie',
                            radius: '50%',
                            data: resData,
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
                // 使用刚指定的配置项和数据显示图表
                myChart.setOption(option);
            },
            error:function(response){             // 网络请求失败，自动调用此方法
                console.log("出错" + response);
            }
        });


    })

</script>
</body>
</html>
